package demo;

import com.google.gson.*;
import java.io.IOException;
import java.util.logging.Logger;
import java.util.*;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class TrackDataServlet extends HttpServlet {
	private static final Logger log = Logger.getLogger(MyTrackServlet.class.getName());	
	
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		TrackDataAO ao = new TrackDataAO();
		Gson gson = new Gson();
		int max = 100;
		String json = null;
		//String imei = "354037050138621";
		
		//changes 3
		String imei = (String)req.getSession().getAttribute("imei");
		
		if (req.getParameter("type") != null) {
			String maxStr = req.getParameter("type");
			try {
				max = Integer.parseInt(maxStr);
			} catch (NumberFormatException e) {
				
			}
			//changes 7
			List<MapData> mList = ao.getData(max, imei);
			//List<MapData> mList = ao.getData(max);
			json = gson.toJson(mList);
			
		} else {
			
			
			//if(imei != null){
			 List<TrackData> list = ao.getAll(imei); 
			//List<TrackData> list = ao.getAll();   ---> changes 4
			json = gson.toJson(list);
		
			//}
		}
		
		resp.setContentType("text/plain");
		resp.getWriter().println("{\"aaData\":" + json +"}");		
		
	}

	public void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		
		doGet(req,resp);
	}

}
